package com.atguigu.gmall.activity.service.impl;

import com.atguigu.gmall.activity.mapper.ActivityRuleMapper;
import com.atguigu.gmall.activity.mapper.ActivitySkuMapper;
import com.atguigu.gmall.activity.mapper.CouponInfoMapper;
import com.atguigu.gmall.activity.service.ActivityRuleService;
import com.atguigu.gmall.model.activity.ActivityRule;
import com.atguigu.gmall.model.activity.ActivityRuleVo;
import com.atguigu.gmall.model.activity.ActivitySku;
import com.atguigu.gmall.model.activity.CouponInfo;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.client.ProductFeignClient;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: zm
 * @Date: 2021/8/15 19:33
 */
@Service
public class ActivityRuleServiceImpl implements ActivityRuleService {

    @Autowired
    private ActivityRuleMapper activityRuleMapper;

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private CouponInfoMapper couponInfoMapper;

    @Autowired
    private ActivitySkuMapper activitySkuMapper;

    // 活动规则List
    @Override
    public Map<String, Object> findActivityRuleList(Long id) {
        Map<String, Object> map = new HashMap<>();
        List<ActivityRule> list = activityRuleMapper.findActivityRuleList(id);

        map.put("activityRuleList", list);

        List<Long> skuIdList = list.get(0).getSkuIdList();
        List<SkuInfo> skuInfoList = productFeignClient.getSkuInfoList(skuIdList);
        map.put("skuInfoList", skuInfoList);

        QueryWrapper<CouponInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("activity_id", id);
        List<CouponInfo> couponInfoList = couponInfoMapper.selectList(wrapper);
        map.put("couponInfoList", couponInfoList);

        return map;
    }

    // 获取 skuInfo 列表
    @Override
    public List<SkuInfo> findSkuInfoByKeyword(String keyWord) {

        return productFeignClient.findSkuInfoByKeyword(keyWord);
    }

    // 保存ActivityRule
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveActivityRule(ActivityRuleVo activityRuleVo) {
        Long activityId = activityRuleVo.getActivityId();

        // ActivityRule
        QueryWrapper<ActivityRule> activityRuleQueryWrapper = new QueryWrapper<>();
        activityRuleQueryWrapper.eq("activity_id", activityId);
        activityRuleMapper.delete(activityRuleQueryWrapper);

        activityRuleVo.getActivityRuleList().stream().forEach((activityRule) -> {
            activityRuleMapper.insert(activityRule);
        });

        // ActivitySku
        QueryWrapper<ActivitySku> activitySkuQueryWrapper = new QueryWrapper<>();
        activitySkuQueryWrapper.eq("activity_id", activityId);
        activitySkuMapper.delete(activitySkuQueryWrapper);
        activityRuleVo.getActivitySkuList().stream().forEach((activitySku) -> {
            activitySku.setActivityId(activityId);
            activitySkuMapper.insert(activitySku);
        });


        List<Long> couponIdList = activityRuleVo.getCouponIdList();
        List<CouponInfo> couponInfos = couponInfoMapper.selectBatchIds(couponIdList);
        couponInfos.stream().forEach((couponInfo)->{
            couponInfo.setActivityId(activityId);
            couponInfoMapper.updateById(couponInfo);
        });


    }


}
